﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.MapEditor.Data;
using AG.Data;

namespace AGMapEditor.Controls
{
    public partial class AGSelectUnitPanel : UserControl
    {
        private AGEMapPanel _ctlMapPanel;

        public AGSelectUnitPanel(AGEMapPanel mapPanel)
        {
            InitializeComponent();

            _ctlMapPanel = mapPanel;
            BindUnitTree();
        }

        private void BindUnitTree()
        {
            _ctlTreeUnit.Nodes.Clear();
            for (int index = 0; index < AGMEContext.Current.Data.UnitCategoryList.Count; index++)
            {
                AGUnitCategory category = AGMEContext.Current.Data.UnitCategoryList[index];
                TreeNode node = new TreeNode();
                _ctlTreeUnit.Nodes.Add(node);

                node.Text = category.ToString();
                node.Tag = category;

                for (int uIndex = 0; uIndex < AGMEContext.Current.Data.UnitList.Count; uIndex++)
                {
                    AGUnit unit = AGMEContext.Current.Data.UnitList[uIndex];
                    if (unit.CategoryId == category.Id)
                    {
                        TreeNode unitNode = new TreeNode();
                        unitNode.Text = unit.ToString();
                        unitNode.Tag = unit;
                        node.Nodes.Add(unitNode);
                    }
                }
            }
        }

        private void _ctlTreeUnit_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                AGUnitCategory unitCategory = e.Node.Parent.Tag as AGUnitCategory;
                AGUnit unit = e.Node.Tag as AGUnit;

                _ctlMapPanel.SetSelectUnit(unitCategory, unit);
            }
        }
    }
}
